home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cvs / dist / src / maketime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-06  |  9.1 KB  |  338 lines

  1. #
  2. /*
  3.  * MAKETIME        derive 32-bit time value from TM structure.
  4.  *
  5.  * Usage:
  6.  *    int zone;    Minutes west of GMT, or
  7.  *            48*60 for localtime
  8.  *    time_t t;
  9.  *    struct tm *tp;    Pointer to TM structure from <time.h>
  10.  *    t = maketime(tp,zone);
  11.  *
  12.  * Returns:
  13.  *    -1 if failure; parameter out of range or nonsensical.
  14.  *    else time-value.
  15.  * Notes:
  16.  *    This code is quasi-public; it may be used freely in like software.
  17.  *    It is not to be sold, nor used in licensed software without
  18.  *    permission of the author.
  19.  *    For everyone's benefit, please report bugs and improvements!
  20.  *     Copyright 1981 by Ken Harrenstien, SRI International.
  21.  *    (ARPANET: KLH @ SRI)
  22.  */
  23. /* $Log:    maketime.c,v $
  24.  * Revision 1.2.1.1  91/01/18  12:15:21  berliner
  25.  * For CVS 1.2, contributed by Paul Eggert
  26.  * 
  27.  * Revision 1.3  1991/01/18  00:14:48  eggert
  28.  * Make minimal changes to interface to CVS 1.0.
  29.  *
  30.  * Revision 5.2  1990/11/01  05:03:30  eggert
  31.  * Remove lint.
  32.  *
  33.  * Revision 5.1  1990/10/04  06:30:13  eggert
  34.  * Calculate the GMT offset of 'xxx LT' as of xxx, not as of now.
  35.  * Don't assume time_t is 32 bits.  Fix bugs near epoch and near end of time.
  36.  *
  37.  * Revision 5.0  1990/08/22  08:12:38  eggert
  38.  * Switch to GMT and fix the bugs exposed thereby.
  39.  * Permit dates past 1999/12/31.  Ansify and Posixate.
  40.  *
  41.  * Revision 1.8  88/11/08  13:54:53  narten
  42.  * allow negative timezones (-24h <= x <= 24h)
  43.  * 
  44.  * Revision 1.7  88/08/28  14:47:52  eggert
  45.  * Allow cc -R.  Remove unportable "#endif XXX"s.
  46.  * 
  47.  * Revision 1.6  87/12/18  17:05:58  narten
  48.  * include rcsparam.h
  49.  * 
  50.  * Revision 1.5  87/12/18  11:35:51  narten
  51.  * maketime.c: fixed USG code - you have tgo call "tzset" in order to have
  52.  * "timezone" set. ("localtime" calls it, but it's probably better not to 
  53.  * count on "localtime" having been called.)
  54.  * 
  55.  * Revision 1.4  87/10/18  10:26:57  narten
  56.  * Updating version numbers. Changes relative to 1.0 are actually 
  57.  * relative to 1.2
  58.  * 
  59.  * Revision 1.3  87/09/24  13:58:45  narten
  60.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  61.  * warnings)
  62.  * 
  63.  * Revision 1.2  87/03/27  14:21:48  jenkins
  64.  * Port to suns
  65.  * 
  66.  * Revision 1.2  83/12/05  10:12:56  wft
  67.  * added cond. compilation for USG Unix; long timezone;
  68.  * 
  69.  * Revision 1.1  82/05/06  11:38:00  wft
  70.  * Initial revision
  71.  * 
  72.  */
  73.  
  74.  
  75. /* minimal changes needed to get this file to work for CVS rather than RCS */
  76. /* #include "rcsbase.h" */
  77. #include <sys/types.h>
  78. #include <time.h>
  79. #include "cvs.h"
  80. #define datesize 32
  81. #define faterror(x,y) error(0,x,y)
  82. #define libId(x,y) static char x[] = y;
  83. #define P(x) ()
  84. #define RCSversion 5
  85. #define VERSION(x) x
  86. #define VOID (void)
  87. #if !__STDC__
  88. #    define const
  89. #endif
  90. void str2date();
  91. Make_Date(a,b) const char *a; char *b; { str2date(a,b); }
  92.  
  93. libId(maketId, "$Id: maketime.c,v 1.2.1.1 91/01/18 12:15:21 berliner Exp $")
  94.  
  95. static const struct tm *time2tm P((time_t));
  96.  
  97. #define given(v) (0 <= (v)) /* Negative values are unspecified. */
  98.  
  99. static const int daytb[] = {
  100.     /* # days in year thus far, indexed by month (0-12!!) */
  101.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  102. };
  103.  
  104.     static time_t
  105. maketime(atm,zone)
  106.     const struct tm *atm;
  107.     int zone;
  108. {
  109.     register const struct tm *tp;
  110.     register int i;
  111.     int year, yday, mon, day, hour, min, sec, leap, localzone;
  112.     int attempts;
  113.     time_t t, tres;
  114.  
  115.     attempts = 2;
  116.     localzone = zone==48*60;
  117.     tres = -1;
  118.     year = mon = day = 0;  /* Keep lint happy.  */
  119.  
  120.     do {
  121.  
  122.     if (localzone || !given(atm->tm_year)) {
  123.         if (tres == -1)
  124.             if ((tres = time((time_t*)0))  ==  -1)
  125.                 return -1;
  126.         tp = time2tm(tres);
  127.         /* Get breakdowns of default time, adjusting to zone. */
  128.         year = tp->tm_year;        /* Use to set up defaults */
  129.         yday = tp->tm_yday;
  130.         mon = tp->tm_mon;
  131.         day = tp->tm_mday;
  132.         hour = tp->tm_hour;
  133.         min = tp->tm_min;
  134.         if (localzone) {
  135.             tp = localtime(&tres);
  136.             zone =
  137.             min - tp->tm_min + 60*(
  138.                 hour - tp->tm_hour + 24*(
  139.                     /* If years differ, it's by one day. */
  140.                         year - tp->tm_year
  141.                     ?    year - tp->tm_year
  142.                     :    yday - tp->tm_yday));
  143.         }
  144.         /* Adjust the default day, month and year according to zone.  */
  145.         if ((min -= zone) < 0) {
  146.             if (hour-(59-min)/60 < 0  &&  --day <= 0) {
  147.             if (--mon < 0) {
  148.                 --year;
  149.                 mon = 11;
  150.             }
  151.             day  =  daytb[mon+1] - daytb[mon] + (mon==1&&!(year&3));
  152.             }
  153.         } else
  154.             if (
  155.               24 <= hour+min/60  &&
  156.               daytb[mon+1] - daytb[mon] + (mon==1&&!(year&3))  <  ++day
  157.             ) {
  158.                 if (11 < ++mon) {
  159.                     ++year;
  160.                     mon = 0;
  161.                 }
  162.                 day = 1;
  163.             }
  164.     }
  165.     if (zone < -24*60  ||  24*60 < zone)
  166.         return -1;
  167.  
  168.  
  169. #ifdef DEBUG
  170. printf("first YMD: %d %d %d\n",year,mon,day);
  171. #endif
  172.     tp = atm;
  173.  
  174.     /* First must find date, using specified year, month, day.
  175.      * If one of these is unspecified, it defaults either to the
  176.      * current date (if no more global spec was given) or to the
  177.      * zero-value for that spec (i.e. a more global spec was seen).
  178.      * Reject times that do not fit in time_t,
  179.      * without assuming that time_t is 32 bits or is signed.
  180.      */
  181.     if (given(tp->tm_year))
  182.       {
  183.         year = tp->tm_year;
  184.         mon = 0;        /* Since year was given, default */
  185.         day = 1;        /* for remaining specs is zero */
  186.       }
  187.     if (year < 69)            /* 1969/12/31 OK in some timezones.  */
  188.         return -1;        /* ERR: year out of range */
  189.     leap   =   !(year&3)  &&  (year%100 || !((year+300)%400));
  190.     year -= 70;            /* UNIX time starts at 1970 */
  191.  
  192.     /*
  193.      * Find day of year.
  194.      */
  195.     {
  196.         if (given(tp->tm_mon))
  197.           {    mon = tp->tm_mon;    /* Month was specified */
  198.             day = 1;        /* so set remaining default */
  199.           }
  200.         if (11 < (unsigned)mon)
  201.             return -1;        /* ERR: bad month */
  202.         if (given(tp->tm_mday)) day = tp->tm_mday;
  203.         if(day < 1
  204.          || (((daytb[mon+1]-daytb[mon]) < day)
  205.             && (day!=29 || mon!=1 || !leap) ))
  206.                 return -1;    /* ERR: bad day */
  207.         yday = daytb[mon]    /* Add # of days in months so far */
  208.           + ((leap        /* Leap year, and past Feb?  If */
  209.               && mon>1)? 1:0)    /* so, add leap day for this year */
  210.           + day-1;        /* And finally add # days this mon */
  211.  
  212.     }
  213.     if (leap+365 <= (unsigned)yday)
  214.         return -1;        /* ERR: bad YDAY */
  215.  
  216.     if (year < 0) {
  217.         if (yday != 364)
  218.         return -1;        /* ERR: too early */
  219.         t = -1;
  220.     } else {
  221.         tres = year*365;        /* Get # days of years so far */
  222.         if (tres/365 != year)
  223.             return -1;        /* ERR: overflow */
  224.         t = tres
  225.         + ((year+1)>>2)        /* plus # of leap days since 1970 */
  226.         + yday;            /* and finally add # days this year */
  227.         if (t+4 < tres)
  228.             return -1;        /* ERR: overflow */
  229.     }
  230.     tres = t;
  231.  
  232.     if (given(i = tp->tm_wday)) /* Check WDAY if present */
  233.         if (i != (tres+4)%7)    /* 1970/01/01 was Thu = 4 */
  234.             return -1;    /* ERR: bad WDAY */
  235.  
  236. #ifdef DEBUG
  237. printf("YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  238. #endif
  239.     /*
  240.      * Now determine time.  If not given, default to zeros
  241.      * (since time is always the least global spec)
  242.      */
  243.     tres *= 86400L;            /* Get # seconds (24*60*60) */
  244.     if (tres/86400L != t)
  245.         return -1;        /* ERR: overflow */
  246.     hour = min = sec = 0;
  247.     if (given(tp->tm_hour)) hour = tp->tm_hour;
  248.     if (given(tp->tm_min )) min  = tp->tm_min;
  249.     if (given(tp->tm_sec )) sec  = tp->tm_sec;
  250.     if (60 <= (unsigned)min  ||  60 < (unsigned)sec)
  251.         return -1;        /* ERR: MS out of range */
  252.     if (24 <= (unsigned)hour)
  253.         if(hour != 24 || (min+sec) !=0)    /* Allow 24:00 */
  254.             return -1;    /* ERR: H out of range */
  255.  
  256.     t = tres;
  257.     tres += sec + 60L*(zone + min + 60*hour);
  258.  
  259. #ifdef DEBUG
  260. printf("HMS: %d %d %d T=%ld\n",hour,min,sec,tres);
  261. #endif
  262.  
  263.     if (!localzone)            /* check for overflow */
  264.         return (year<0 ? (tres<0||86400L<=tres) : tres<t)  ?  -1  :  tres;
  265.  
  266.     /* Check results; LT may have had a different GMT offset back then.  */
  267.     tp = localtime(&tres);
  268.     if (given(atm->tm_sec)  &&  atm->tm_sec != tp->tm_sec)
  269.         return -1; /* If seconds don't match, we're in trouble.  */
  270.     if (!(
  271.         given(atm->tm_min)  &&  atm->tm_min != tp->tm_min  ||
  272.         given(atm->tm_hour)  &&  atm->tm_hour != tp->tm_hour  ||
  273.         given(atm->tm_mday)  &&  atm->tm_mday != tp->tm_mday  ||
  274.         given(atm->tm_mon)  &&  atm->tm_mon != tp->tm_mon  ||
  275.         given(atm->tm_year)  &&  atm->tm_year != tp->tm_year
  276.     ))
  277.         return tres; /* Everything matches.  */
  278.  
  279.     } while (--attempts);
  280.  
  281.     return -1;
  282. }
  283.  
  284. /*
  285. * Convert Unix time to struct tm format.
  286. * Use Coordinated Universal Time (UTC) if available and if version 5 or newer;
  287. * use local time otherwise.
  288. */
  289.     static const struct tm *
  290. time2tm(unixtime)
  291.     time_t unixtime;
  292. {
  293.     const struct tm *tm;
  294.     return
  295.             VERSION(5)<=RCSversion && (tm = gmtime(&unixtime))
  296.         ?    tm
  297.         :    localtime(&unixtime);
  298. }
  299.  
  300. /*
  301. * Convert Unix time to RCS format.
  302. * For compatibility with older versions of RCS,
  303. * dates before AD 2000 are stored without the leading "19".
  304. */
  305.     void
  306. time2date(unixtime,date)
  307.     time_t unixtime;
  308.     char date[datesize];
  309. {
  310.     register const struct tm *tm = time2tm(unixtime);
  311.     VOID sprintf(date, DATEFORM,
  312.         tm->tm_year  +  (tm->tm_year<100 ? 0 : 1900),
  313.         tm->tm_mon+1, tm->tm_mday,
  314.         tm->tm_hour, tm->tm_min, tm->tm_sec
  315.     );
  316. }
  317.  
  318.  
  319.  
  320.     void
  321. str2date(source, target)
  322.     const char *source;
  323.     char target[datesize];
  324. /* Parse a free-format date in SOURCE, convert it
  325.  * into RCS internal format, and store the result into TARGET.
  326.  */
  327. {
  328.     int zone;
  329.     time_t unixtime;
  330.     struct tm parseddate;
  331.  
  332.     if (!partime(source, &parseddate, &zone))
  333.         faterror("can't parse date/time: %s", source);
  334.     if ((unixtime = maketime(&parseddate, zone))  ==  -1)
  335.         faterror("bad date/time: %s", source);
  336.     time2date(unixtime, target);
  337. }
  338.